/**
 * 信息统计，主要统计企业和项目的数量
 */
var aggregations = {
    data : {
        MODULE_ID : "",
        chart : null
    },
    init : function() {
        aggregations.data.MODULE_ID = GetRequest().pid;
        aggregations.getAllUsers();
        // 基于准备好的dom，初始化echarts实例
        aggregations.data.chart = echarts.init(document.getElementById('chart'), 'macarons');
        $("#select").change(function() {
            aggregations.getSelected();
        });
        $("#select_user").change(function() {
            aggregations.getSelected();
        });
    },
    getSelected : function() {
        var select = $("#select").children('option:selected');
        var selected_user = $("#select_user").val();
        var selected = select.val();
        var selected_text = select.text();

        if(isBlank(selected_user)) { // 没有选择用户
            if(selected === "ent_create_date") {
                aggregations.generateChart("bar", "/crm/statistics/enterprise", {uuid : null}, selected_text);
            } else if(selected === "pro_create_date") {
                aggregations.generateChart("bar", "/crm/statistics/project", {uuid : null}, selected_text);
            }
        } else { // 选择了用户
            if(selected === "ent_create_date") {
                aggregations.generateChart("bar", "/crm/statistics/enterprise", {uuid : selected_user}, selected_text);
            } else if(selected === "pro_create_date") {
                aggregations.generateChart("bar", "/crm/statistics/project", {uuid : selected_user}, selected_text);
            }
        }
    },
    /**
     * 获取所有可用用户
     */
    getAllUsers : function() {
        $.ajax({
            url : "/crm/user/loadUserByIsUse",
            dataType : "json",
            async : false,
            data : {isUse : 1},
            type : "post",
            success : function(res) {
                if(res.length > 0) {
                    var options = "";
                    for(var i = 0 ; i < res.length ; i ++) {
                        options += '<option value="' + res[i].uuid + '">' + res[i].userRealName + '</option>'
                    }
                    $("#select_user").append(options);
                }
            },
            error : function() {
                layer.msg("系统错误！", {icon: 2, closeBtn: 0});
            }
        });
    },
    /**
     * 生成图表，传入参数
     * @param type 图标类型：饼状图 / 柱状图
     * @param url 接口url
     * @param data 传的用户uuid
     * @param text 图表标题
     */
    generateChart : function(type, url, data, text){
        if(type === "pie") {
            $.get(url, data, function(res) {
                if(res && res.data && res.keys) {
                    // 饼状图
                    var option = {
                        backgroundColor: '#fff',
                        title: {
                            text: text
                        },
                        tooltip: {},
                        toolbox: {
                            feature: {
                                saveAsImage: {}
                            }
                        },
                        grid: {
                            top: '10%',
                            left: '3%',
                            right: '5%',
                            bottom: '3%',
                            containLabel: true
                        },
                        legend: {
                            orient: 'vertical',
                            x: 'left',
                            top: '10%',
                            data: res.keys
                        },
                        series: [{
                            name: '数量/占比',
                            type: 'pie',
                            center: ['60%', '50%'],
                            radius: '70%',
                            label: {
                                normal: {
                                    formatter: '{a|{a}}{abg|}\n{hr|}\n  {b|{b}：}{c}  {per|{d}%}  ',
                                    backgroundColor: '#eee',
                                    borderColor: '#aaa',
                                    borderWidth: 1,
                                    borderRadius: 4,
                                    // shadowBlur:3,
                                    // shadowOffsetX: 2,
                                    // shadowOffsetY: 2,
                                    // shadowColor: '#999',
                                    // padding: [0, 7],
                                    rich: {
                                        a: {
                                            color: '#999',
                                            lineHeight: 22,
                                            align: 'center'
                                        },
                                        // abg: {
                                        //     backgroundColor: '#333',
                                        //     width: '100%',
                                        //     align: 'right',
                                        //     height: 22,
                                        //     borderRadius: [4, 4, 0, 0]
                                        // },
                                        hr: {
                                            borderColor: '#aaa',
                                            width: '100%',
                                            borderWidth: 0.5,
                                            height: 0
                                        },
                                        b: {
                                            fontSize: 16,
                                            lineHeight: 33
                                        },
                                        per: {
                                            color: '#eee',
                                            backgroundColor: '#334455',
                                            padding: [2, 4],
                                            borderRadius: 2
                                        }
                                    }
                                }
                            },
                            data: res.data,
                            itemStyle: {
                                emphasis: {
                                    shadowBlur: 10,
                                    shadowOffsetX: 0,
                                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                                }
                            }
                        }]
                    };
                    aggregations.data.chart.clear();
                    // 使用刚指定的配置项和数据显示图表。
                    aggregations.data.chart.setOption(option);
                }
            })
        } else if(type === "bar") { // 柱状图
            $.get(url, data, function (res) {
                if(res && res.data.length > 0) {
                    var keys = [];
                    var data = [];
                    var startText = res.data[0].name;
                    var endText = "";
                    if(res.data.length >= 30) {
                        endText = res.data[29].name;
                    } else {
                        endText = res.data[res.data.length - 1].name;
                    }
                    for (var i = 0; i < res.data.length; i++) {
                        keys.push(res.data[i].name);
                        data.push(res.data[i].value);
                    }
                }
                var option = {
                    backgroundColor: '#fff',
                    title: {
                        text: text
                    },
                    tooltip: {},
                    toolbox: {
                        feature: {
                            saveAsImage: {}
                        }
                    },
                    grid: {
                        left: '3%',
                        right: '5%',
                        bottom: '3%',
                        containLabel: true
                    },
                    dataZoom : {
                        show : true,
                        realtime : true,
                        startValue: startText,
                        endValue: endText
                    },
                    xAxis: {
                        axisLabel: {
                            interval: 0,    //强制文字产生间隔
                            rotate: 45,     //文字逆时针旋转45°
                            textStyle: {    //文字样式
                                color: "black",
                                fontSize: 12,
                                fontFamily: 'Microsoft YaHei'
                            }
                        },
                        data: keys
                    },
                    yAxis: [
                        {
                            type: 'value'
                        }
                    ],
                    series: [{
                        name: '数量',
                        type: 'bar',
                        label: {
                            normal: {
                                show: true,
                                position: 'inside'
                            }
                        },
                        barWidth: '70%',
                        data: data
                    }]
                };
                aggregations.data.chart.clear();
                // 使用刚指定的配置项和数据显示图表。
                aggregations.data.chart.setOption(option);
            })
        }
    }
};